package com.study.spark.scala.nginx.util

import java.net.URL

/**
  *
  * @author: stephen.shen
  * @create: 2019-03-08 17:24
  */
object UrlUtils {

  /**
    * 从URL中提取域名或者IP
    *
    * @param url
    * @return
    */
  def getDomain(url: String): String = {
    try {
      new URL(url).getHost
    } catch {
      case ex: Exception => "-"
    }
  }

  /**
    * 去掉URL后面的参数
    *
    * @param url
    * @return
    */
  def clearParam(url: String): String = {
    if (url.indexOf("?") != -1) {
      try {
        url.split("[?]")(0)
      } catch {
        case ex: Exception => {
          ex.printStackTrace()
          url
        }
      }
    } else {
      url
    }
  }

  /**
    * 把“/”替换成“/index.html”
    * @param url
    * @return
    */
  def replaceIndex(url:String):String={
    if(url.equals("/")){
      return "/index.html"
    }
    url
  }

  def main(args: Array[String]): Unit = {
    val url = "192.168.81.5:8080/productlist.html?parentCategoryName=S001"
    //println(getDomain(url))
    println(clearParam(url))
  }



}
